<?php

namespace app\index\service;

use app\common\model\Member;
use app\index\dao\MemberDao;
use app\index\traits\SessionAfterLocationTrait;
use app\index\traits\SessionTrait;
use ShidianUtil\Captcha\Captcha;
use ShidianUtil\Utils\CheckUtil;
use ShidianUtil\Utils\StringUtil;
use think\exception\ValidateException;

/**
 * 抖音登录
 * Class SessionDouyinService
 * @package app\index\service
 */
class SessionDouyinService extends AbstractSessionService
{
    use SessionTrait;
    use SessionAfterLocationTrait;

    private MemberService $memberService;
    private MemberDao $memberDao;
    private Captcha $captcha;

    public function __construct(MemberService $memberService, MemberDao $memberDao, Captcha $captcha)
    {
        $this->memberService = $memberService;
        $this->memberDao = $memberDao;
        $this->captcha = $captcha;
    }

    /**
     * 抖音小程序登录
     * @param $params
     * @return Member
     */
    public function login($params): Member
    {
        $unionidDouyin = $params['unionidDouyin'] ?? null;
        $openid = $params['openidDouyin'] ?? null;
        $phone = $params['phone'] ?? null;
        $pf = $params['pf'];
        $chain = $params['chain'];
        $promotionGroupId = $params['promotionGroupId'];

        if (StringUtil::isBlank($unionidDouyin) || StringUtil::isBlank($openid)) {
            throw new ValidateException('请允许抖音小程序授权');
        }

        if (is_blank($phone) || !CheckUtil::isPhoneNumber($phone)) {
            throw new ValidateException('请输入手机号码');
        }

        // 检查律师认证资料
        $this->checkMemberDraft($params['memberDraft'] ?? null);

        // 三方平台身份标志字段
        $bindKeys = ['unionidDouyin', 'openidDouyin',];

        // 用户信息字段
        $memberParamsKeys = array_merge($bindKeys, ['phone', 'nickname', 'headImgUrl', 'inviteCode']);

        // 传进来的用户信息参数
        $memberParams = collect_params($params, $memberParamsKeys);

        // 注册方式: 0/其它,1/qq,2/支付宝,3/微信pc扫码,4/微信小程序,5/微信公众号关注,6/苹果App,7/安卓App,8/百度小程序,9/抖音小程序
        $memberParams['channel'] = 9;
        $memberParams['pf'] = $pf;
        $memberParams['chain'] = $chain;
        $memberParams['promotionGroupId'] = $promotionGroupId;

        // 查询用户
        $member = $this->findMember('unionidDouyin', $unionidDouyin, $phone, $memberParams, $bindKeys);

        // 是否冻结
        $this->checkMemberStatus($member);

        // 提交律师认证资料
        $this->saveMemberDraft($member,$params['memberDraft'] ?? null, $pf, $chain, $promotionGroupId);

        // **************** 登录并返回 ****************

        // 设置定位
        $this->setLocation($member);

        // 查询前端用户信息
        $member = $this->memberDao->findByLogin($member->id);

        // 刷新token令牌
        $token = $this->getMemberTokenService()->add($member->id, $memberParams['channel'], 3);

        // 给前端的参数，包装一下
        $this->wrapToken($member, $token);

        return $member;
    }


    private function getMemberTokenService(): MemberTokenService
    {
        return app(MemberTokenService::class);
    }
}
